package com.traffic.between.adaptor;

import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.HaiHangServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;
@ChooseProvider(channel = "haihang")
public class HaiHangAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(HaiHangAdaptor.class);

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/test")
	public String getResult(@QueryParam("name") String name) {
		return name;
	}
	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
	
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
       
		log.info("between- 海航流量订购接口请求参数productId:" + productId+",phoneNo:"+phoneNo+",cstmOrderNo:"+cstmOrderNo);
		String result = HaiHangServiceImpl.service.order(productId, phoneNo, cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("resultCode");
	

		if (("00".equals(code))) {
			
			orderBean.setStatus("11");
			orderBean.setMessage(json.getString("resultDesc"));
			orderBean.setErrorMsg("订购中");
			
		} else {
			
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("resultDesc"));
			orderBean.setErrorMsg("订购失败");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
		
		OrderBean orderBean = OrderInfoContext.getContext();
		String cstmOrderNo = orderBean.getIds();
		log.info("between- 海航流量查询状态请求参数cstmOrderNo："+cstmOrderNo);
		String result = HaiHangServiceImpl.service.orderStatus(cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("resultCode");
		if (("00".equals(code))) {
			
			orderBean.setStatus("7");
			orderBean.setMessage(json.getString("resultDesc"));
			orderBean.setErrorMsg("充值成功");
		} else if("01".equals(code)) {
			
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("resultDesc"));
			orderBean.setErrorMsg("订购失败");
		}else{
			
			orderBean.setStatus("1");
			orderBean.setMessage(json.getString("resultDesc"));
			orderBean.setErrorMsg("订购中");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
